import {Injectable} from '@angular/core';
import {HttpUtilService} from "../../../../../services/http/http-util.service";
import {BarnRole} from "../entity/barn-role";
import {Observable} from "rxjs";
import {ResponseEntity} from "../../../../../services/http/response-entity";
import {RoleUser} from "../entity/role-user";
import {RoleMenu} from "../entity/role-menu";
import {environment} from "../../../../../../environments/environment";

@Injectable({
  providedIn: 'root'
})
export class RoleInfoService {

  private prefix = environment.serverName.system + "/role"

  constructor(private http: HttpUtilService) {
  }

  /**
   * 保存
   * @param data new data
   * @param handleError
   */
  save(data: BarnRole, handleError = true): Observable<BarnRole> {
    return this.http.post<BarnRole>(this.prefix + "/save", data, handleError)
  }

  /**
   * 条件查询
   * @param param 查询条件
   * @param current 分页 当前页
   * @param pageSize 分页大小
   * @param handleError
   */
  query(param: BarnRole, current = 0, pageSize = 0, handleError = true): Observable<ResponseEntity<BarnRole[]>> {
    return this.http.postPage(this.prefix + "/query", param, current, pageSize, handleError)
  }

  /**
   * 主键查询
   * @param pid
   * @param handleError
   */
  getById(pid: string, handleError = true): Observable<BarnRole> {
    return this.http.get(this.prefix + "/getById/" + pid, handleError)
  }

  /**
   * 主键删除
   * @param pid
   * @param handleError
   */
  deleteById(pid: string, handleError = true): Observable<any> {
    return this.http.get(this.prefix + "/delete/" + pid, handleError)
  }

  /**
   * 查找用户的角色
   * @param userId user id
   * @param handleError
   */
  getByUserId(userId: string, handleError = true): Observable<BarnRole[]> {
    return this.http.get(this.prefix + "/getByUserId/" + userId, handleError);
  }

  /**
   * 查询某个角色的用户情况
   * @param param
   * @param handleError
   * @param current
   * @param pageSize
   */
  getUsers(param: RoleUser, current = 0, pageSize = 0, handleError = true): Observable<ResponseEntity<RoleUser[]>> {
    return this.http.getPage(this.prefix + "/getUsers", current, pageSize, handleError, param)
  }

  /**
   * 查询某个角色的菜单
   * @param param
   * @param current
   * @param pageSize
   * @param handleError
   */
  getMenus(param: RoleMenu, current = 0, pageSize = 0, handleError = true): Observable<RoleMenu[]> {
    return this.http.get(this.prefix + "/getMenus", handleError, param)
  }

  /**
   * 查找 team 的默认角色
   * @param teamId team id
   * @param handleError
   */
  getByTeamId(teamId: string, handleError = true): Observable<BarnRole[]> {
    return this.http.get(this.prefix + "/getByTeamId/" + teamId, handleError);
  }

  /**
   * 查找用户所在team的默认角色
   * @param userId user id
   * @param handleError
   */
  getByUserTeam(userId: string, handleError = true): Observable<BarnRole[]> {
    return this.http.get(this.prefix + "/getByUserTeam/" + userId, handleError);
  }
}
